from fastapi import APIRouter, Request
from fastapi import Depends
from config.get_db import get_db
from module_admin.entity.vo.cms_car_vo import CarModel, CarPageObject, CarForQuery
from module_admin.entity.vo.cms_home_vo import CarKanban, MyAgentSum, HomeStatsSum, DriverKanban, HomeStatsSumToday, \
    OrderStartAndMileage
from module_admin.service.cms_car_service import CmsCarService
from module_admin.dao.dept_dao import *
from module_admin.service.cms_home_service import CmsHomeService
from module_admin.service.login_service import get_current_user_details
from utils.page_util import get_page_obj
from utils.response_util import *
from utils.log_util import *
from utils.time_format_util import object_format_datetime

homeController = APIRouter(dependencies=[Depends(get_current_user_details)])


@homeController.get("/stats/sum", response_model=HomeStatsSum, summary="首页-统计信息")
async def stats_sum(request: Request, query_db: Session = Depends(get_db)):
    try:
        result = CmsHomeService.stats_sum(query_db)
        return response_200(data=result, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@homeController.get("/my/agent", response_model=MyAgentSum, summary="首页-我的代办")
async def my_agent(request: Request, query_db: Session = Depends(get_db)):
    try:
        result = CmsHomeService.my_agent(query_db)
        return response_200(data=result, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@homeController.get("/stats/sumToday", response_model=HomeStatsSumToday, summary="首页-今日统计")
async def stats_sum(request: Request, query_db: Session = Depends(get_db)):
    try:
        result = CmsHomeService.stats_sum_today(query_db)
        return response_200(data=result, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@homeController.get("/car/kanban", response_model=CarKanban, summary="首页-用车看板")
async def car_kanban(request: Request, query_db: Session = Depends(get_db)):
    try:
        result = CmsHomeService.car_kanban(query_db)
        return response_200(data=result, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@homeController.get("/driver/kanban", response_model=DriverKanban, summary="首页-驾驶员看板")
async def car_kanban(request: Request, query_db: Session = Depends(get_db)):
    try:
        result = CmsHomeService.driver_kanban(query_db)
        return response_200(data=result, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))

@homeController.get("/orderStartAndMileage", response_model=OrderStartAndMileage, summary="首页-出车次数和里程统计")
async def stats_sum(request: Request, query_db: Session = Depends(get_db)):
    try:
        result = CmsHomeService.order_start_and_mileage(query_db)
        list_format_datetime(result)
        return response_200(data=result, message="获取成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
